package com.csnt.dblocation.httpclient;

import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

/***
 * 设置 http客户端 请求类型
 *
 */
@Service
public class HttpClientApiUtils {

    @Autowired
    private CloseableHttpClient httpClient;
    @Autowired
    private RequestConfig config;

    /**
     * 不带参数的get请求，如果状态码为200，则返回body，如果不为200，则返回null
     *
     * @param url 请求路径
     * @return 响应内容
     * @throws Exception 抛异常
     */
    public String doGet(String url) throws Exception {
        // 声明 http get请求
        HttpGet httpGet = new HttpGet(url);
        System.out.println(url);
        // 装载配置信息
        httpGet.setConfig(config);
        //发起请求
        CloseableHttpResponse response = this.httpClient.execute(httpGet);
        //判断状态码是否为200
        if (response.getStatusLine().getStatusCode() == 200) {
            //返回响应体内容
            return EntityUtils.toString(response.getEntity(), "UTF-8");
        }
        return null;
    }

    /**
     * 带参数的get请求
     *
     * @param url 请求路径
     * @param map map 集合
     * @return 响应内容
     * @throws Exception 抛异常
     */
    public String doGet(String url, Map<String, Object> map) throws Exception {
        URIBuilder uriBuilder = new URIBuilder(url);
        if (map != null) {
            //遍历map,拼接请求参数
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
            }
        }
        //调用不带参数get请求
        return this.doGet(uriBuilder.build().toString());
    }

    /**
     * 带参数的post请求
     * @param url 请求路径
     * @param map 入参
     * @return *
     * @throws IOException *
     */
    public JsonResult doPost(String url, Map<String,Object> map) throws IOException {
        //声明httpPost请求
        HttpPost httpPost = new HttpPost(url);
        //加入配置信息
        httpPost.setConfig(config);
        //判断map是否为空，不为空则进行遍历，封装from表单对象
        if (map!=null){
            ArrayList<NameValuePair> list = new ArrayList<>();
            for (Map.Entry<String,Object> entry:map.entrySet()){
                list.add(new BasicNameValuePair(entry.getKey(),entry.getValue().toString()));
            }
            UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
            // 把表单放到post里
            httpPost.setEntity(urlEncodedFormEntity);
        }
        //发送请求
        CloseableHttpResponse response = httpClient.execute(httpPost);
        return new JsonResult<>(response.getStatusLine().getStatusCode(),"发送请求",EntityUtils.toString(response.getEntity(),"UTF-8"),0);
    }

    /**
     * 不带参数的post请求
     * @param url 请求路径
     * @return *
     * @throws IOException *
     */
    public JsonResult doPost(String url) throws IOException {
        return this.doPost(url,null);
    }







}
